#include "Ano_Data.h"
#include "main.h"
#include "stdio.h"
#include "usart.h"

extern uint8_t line[100][2];
extern uint8_t flag_recved_animal;
int32_t uav_location_x;
int32_t uav_location_y;
uint8_t animal_num_inblock;

int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 1000);
  return ch;
}


uint8_t sum_check = 0;
uint8_t add_check = 0;
uint8_t Data_check(uint8_t *data, uint8_t length)
{
	
  for(uint16_t s = 0; s < length; s++)
	{
		sum_check += data[s];
		add_check += sum_check;
	}
	if((sum_check==data[length])&&(add_check==data[length+1]))
	return 1;
	else return 0;
}

uint8_t Buffer_fly[20];
uint8_t Rx_fly;
uint8_t ig = 0;					//!
uint8_t A_location,B_location; 
uint8_t Send_flag = 0;
void Datasend8_t(uint8_t data_r)
{
	HAL_UART_Transmit(&huart3,&data_r, 1, 1);
	HAL_Delay(10);
}



uint8_t Fly_protocol[11] = {0xAA, 0x05, 0xE0, 0x05, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};

void FlyDataSend(uint8_t location_num,uint8_t Aim)
{
	Fly_protocol[6] = Aim;
	Fly_protocol[7] = 0xA0+10-(line[location_num][0]+1);
	Fly_protocol[8] = 0xB0+line[location_num][1]+1;
	Data_check(Fly_protocol,9);
	Fly_protocol[9] = sum_check;
	Fly_protocol[10] = add_check;
	sum_check=0;
	add_check=0;
	for(uint16_t j = 0; j < 11; j++)
	{
		Datasend8_t(Fly_protocol[j]);
	}
}







uav_str UAV;

animal_str animal;

#define S32(a, b, c, d) (((int32_t)(a) << 24) | ((int32_t)(b) << 16) | ((int32_t)(c) << 8) | (int32_t)(d))
#define DT_RX_BUFNUM 100

void AnoDTDataAnl(u8 *data,u8 len)
{
	u8 check_sum1 = 0,check_sum2 = 0;
	if(*(data+3) != (len-6))	//�ж����ݳ����Ƿ���ȷ
	{
		return;
	}
  for(u8 i=0; i<len-2; i++)
	{
        check_sum1 += *(data+i);
		check_sum2 += check_sum1;
	}
	if((check_sum1 != *(data+len-2)) || (check_sum2 != *(data+len-1)))	//�ж�sumУ��
	{
    return;
	}
	//=============================================================================
	if(*(data+2)==0X30) //收到animal数据认为到达点完成采集
	{
		animal.Loca_x=*(data+4);
		animal.Loca_y=*(data+5);
		animal.animal_type=*(data+6);
		animal.animal_num=*(data+7);
		if(animal.Loca_x==0 && animal.Loca_y==0)
		{
			animal_num_inblock=	animal.animal_num;//animal在一个格子内的总数
		}
//		else{
//			Store_animal_info(animal);//整理animal结构体进行存储
//			animal_num_inblock--;
//		if(animal_num_inblock==0){
//			flag_recved_animal=1;//总数归零接收完成，也可能重复接收
//		}
//		}


	}
	if(*(data+2)==0X32) //��������
	{
		UAV.x=S32(*(data+7),*(data+6),*(data+5),*(data+4));
		UAV.y=S32(*(data+11),*(data+10),*(data+9),*(data+8));

	}	
}
//===========================================================
//���ݲ�ͬͨ�ŷ�ʽ������������ݻ�����
//===========================================================
typedef struct
{
	u8 DT_RxBuffer[DT_RX_BUFNUM];
	u8 _data_len;
	u8 _data_cnt;
	u8 rxstate;
}_dt_rx_anl_st;


void AnoDTRxOneByte(u8 data)
{
	static _dt_rx_anl_st rx_anl;

	
	switch (rx_anl.rxstate)
	{
		case 0:
		{
			if(data==0xAA)
			{
				rx_anl.rxstate=1;
				rx_anl.DT_RxBuffer[0]=data;
			}
		}
		break;
		case 1:
		{
			if((data == 0xFF || data == 0xAF || data == 0x05))
			{
				rx_anl.rxstate=2;
				rx_anl.DT_RxBuffer[1]=data;
			}
			else
			{
				rx_anl.rxstate = 0;
			}			
		}
		break;
		case 2:
		{
			rx_anl.rxstate = 3;
			rx_anl.DT_RxBuffer[2]=data;	
		}
		break;
		case 3:
		{
			if(data<(DT_RX_BUFNUM-5))
			{
				rx_anl.rxstate = 4;
				rx_anl.DT_RxBuffer[3]=data;
				rx_anl._data_len = data;
				rx_anl._data_cnt = 0;
			}
			else
			{
				rx_anl.rxstate = 0;
			}			
		}
		break;
		case 4:
		{
			if(rx_anl._data_len>0)
			{
				rx_anl._data_len--;
				rx_anl.DT_RxBuffer[4+rx_anl._data_cnt++]=data;
				if(rx_anl._data_len==0)
				{
					rx_anl.rxstate = 5;
				}
			}
			else
			{
				rx_anl.rxstate = 0;
			}
		}
		break;
		case 5:
		{
			rx_anl.rxstate = 6;
			rx_anl.DT_RxBuffer[4+rx_anl._data_cnt++]=data;		
		}
		break;		
		case 6:
		{
			u8 _data_cnt;
			rx_anl.rxstate = 0;
			rx_anl.DT_RxBuffer[4+rx_anl._data_cnt]=data;
			_data_cnt = rx_anl._data_cnt+5;
			//ano_dt_data_ok = 1;
			AnoDTDataAnl(rx_anl.DT_RxBuffer, _data_cnt);		
		}
		break;	
		default :
		{
			rx_anl.rxstate = 0;
		}break;
	}
}

//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
//	if(huart == &huart3) 
//	{
//		AnoDTRxOneByte(Rx_fly);

//		HAL_UART_Receive_IT(&huart3,&Rx_fly,1);
//	}
//	
//}
